package com.fd.wpx.sso.realm;


import com.fd.wpx.authorizating.service.SysUserService;
import com.fd.wpx.blog.pojo.SysUser;
import com.fd.wpx.common.pojo.BlogResult;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

public class UserRealm extends AuthorizingRealm {
    @Autowired
    public SysUserService userService;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("此方法永远不会被执行");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String) token.getPrincipal();
        BlogResult blog = userService.findUserByName(username);
        if (blog.getStatus() == 404)
        {
            throw new UnknownAccountException();//没找到帐号
        }
        SysUser user=(SysUser)blog.getData();
        if(user.getIslock().equals("0"))
        {
            throw new LockedAccountException(); //帐号锁定
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                user.getUsername(), //用户名
                user.getPassword(), //密码
                getName()  //realm name
        );
        return authenticationInfo;
    }
}
